package org.nobject.common.mail;

import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {

	/**
	 * 发送邮件的props文件
	 */
	private final transient Properties props = System.getProperties();
	/**
	 * 邮件服务器登录验证
	 */
	private transient MailAuthenticator authenticator;

	/**
	 * 邮箱session
	 */
	private transient Session session;

	/**
	 * 初始化邮件发送器
	 * 
	 * @param smtpHostName SMTP邮件服务器地址
	 * @param username 发送邮件的用户名(地址)
	 * @param password 发送邮件的密码
	 */
	public MailUtils(String smtpHostName,String username,String password) {
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtpHostName);
		authenticator = new MailAuthenticator(username, password);
		session = Session.getInstance(props, authenticator);
	}

	/**
	 * 初始化邮件发送器
	 * 
	 * @param username 发送邮件的用户名
	 * @param password 发送邮件的密码
	 */
	public MailUtils(String username, String password) {
		this("smtp." + username.split("@")[1],username, password);
	}

	public static void main(String[] args) throws Exception {
		MailUtils.send("smtp.163.com", "nobject_mail@163.com", "!@#$%^&*(","30974621@qq.com", "@@!@!#@#!@#","asdasdasdasdasd");
		System.out.println("OVER");
	}


	/**
	 * 发送邮件
	 * 
	 * @param recipient 收件人邮箱地址
	 * @param subject 邮件主题
	 * @param content 邮件内容
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public void send(String recipient, String subject, Object content) throws AddressException, MessagingException {
		// 创建mime类型邮件
		final MimeMessage message = new MimeMessage(session);
		// 设置发信人
		message.setFrom(new InternetAddress(authenticator.username));
		// 设置收件人
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
		// 设置主题
		message.setSubject(subject);
		// 设置邮件内容
		message.setContent(content.toString(), "text/html;charset=utf-8");
		// 发送
		Transport.send(message);
	}

	/**
	 * 群发邮件
	 * 
	 * @param recipients 收件人们
	 * @param subject 主题
	 * @param content 内容
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public void send(List<String> recipients, String subject, Object content) throws AddressException, MessagingException {
		// 创建mime类型邮件
		final MimeMessage message = new MimeMessage(session);
		// 设置发信人
		message.setFrom(new InternetAddress(authenticator.username));
		// 设置收件人们
		final int num = recipients.size();
		InternetAddress[] addresses = new InternetAddress[num];
		for (int i = 0; i < num; i++) {
			addresses[i] = new InternetAddress(recipients.get(i));
		}
		message.setRecipients(RecipientType.TO, addresses);
		message.setSubject(subject);
		message.setContent(content.toString(), "text/html;charset=utf-8");
		
		Transport.send(message);
	}

	/** 
	 * send
	 * @param host
	 * @param username
	 * @param password
	 * @param recver
	 * @param title
	 * @param content
	 * @throws MessagingException
	 */
	public static void send(String host, String username, String password,String recver,String title,String content) throws MessagingException{
		MailUtils mail=new MailUtils(host,username, password);
		mail.send(recver, title, content);
	}
	
	/** 
	 * send
	 * @param host
	 * @param username
	 * @param password
	 * @param recvers
	 * @param title
	 * @param content
	 * @throws MessagingException
	 */
	public static void send(String host, String username, String password,List<String> recvers,String title,String content) throws MessagingException{
		MailUtils mail=new MailUtils(host,username, password);
		mail.send(recvers, title, content);
	}

}

/**
 * MailAuthenticator
 *
 * @author bianrongjun
 * @version 1.0
 */
class MailAuthenticator extends Authenticator {
	
	/** 用户名 */
	public String username;
	/** 密码 */
	public String password;

	/**
	 * 初始化邮箱和密码
	 * @param username 邮箱
	 * @param password 密码
	 */
	public MailAuthenticator(String username, String password) {
		this.username = username;
		this.password = password;
	}

	/* (non-Javadoc)
	 * @see javax.mail.Authenticator#getPasswordAuthentication()
	 */
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(username, password);
	}
}
